<?php

// Include
include_once('include/header.php');
include_once('plugin/PlanesDBPlugin.php');
include_once('plugin/AppConfigPlugin.php');

// Breadcrumb
$breadcrumb = AppConfigPlugin::getBreadcrumb(basename($_SERVER['PHP_SELF']));
$smarty->assign('breadcrumb', $breadcrumb);


// Eliminar
if (isset($_POST[sendform])) {
	if (isset($_POST[action]) && ($_POST[action] == 'removeall')) {
		$message = PlanesDBPlugin::removePlanes($_POST[iditems]);
		$count = count(explode(".", $_POST[iditems]));
		if ($count == 1) {
			CommonPlugin::setMessage("El plan se ha eliminado correctamente");
		}
		else if ($count > 1) {
			CommonPlugin::setMessage("Los planes se han eliminado correctamente");
		}
		header( 'Location: planes.php' );
		exit;
	}	
}



// Paginador
$totalItemsPerPage = AppConfigPlugin::getTotalItemsPerPage();
$page = $_GET[page];
if (!$page) {
	$start = 0;
	$page = 1;
}
else
	$start = ($page - 1) * $totalItemsPerPage;



// Ejecucion
$results = PlanesDBPlugin::getPlanes($start, $totalItemsPerPage);
$resultsCount = PlanesDBPlugin::getPlanesCount();


// Cantidad total de paginas
$totalPages = ceil($resultsCount / $totalItemsPerPage);


// Fetch
$planes = array();
while($plan = mysql_fetch_assoc($results)) {
	$planes[] = $plan;
}


// Smarty call
$smarty->assign('planes', $planes);
$smarty->assign('page', $page);
$smarty->assign('totalPages', $totalPages);
$smarty->assign('planesCount', $resultsCount);

$smarty->display('planes.tpl');

?>